<?php
/**
 * YICMS
 * ============================================================================
 * 版权所有 2014-2017 YICMS，并保留所有权利。
 * 网站地址: http://www.yicms.vip
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Created by PhpStorm.
 * Author: kenuo
 * Date: 2017/11/17
 * Time: 下午4:38
 */

namespace App\Services\PayAndConsume;

use Auth;
use Route;
use Zhuzhichao\IpLocationZh\Ip;
use App\Repositories\PayAndConsume\PayRepository;
use Cache;

class PayService
{

    protected $payRepository;

    /**
     * ActionLogsService constructor.
     * @param $actionLogsRepository
     */
    public function __construct(PayRepository $payRepository)
    {
        $this->payRepository = $payRepository;
    }

    /**
     * 根据服务器ID获取充值排行
     */
    public function getPayRankByUniqueID($request)
    {
        $payRank = $this->payRepository->getWithPayRank($request);

        return $payRank;
    }

    /**
     * 根据账号获取 充值详情
     */
    public function getDetailByAccount($request)
    {
        $data = $this->payRepository->getWithDetail($request);

        return $data;
    }

    /**
     * 根据服务器ID获取交易日志
     */
    public function getTradeLogByUniqueID($request)
    {
        $payRank = $this->payRepository->getWithTradeLog($request);

        return $payRank;
    }

    /**
     * 根据服务器ID获取 充值档位
     */
    public function getGradeByUniqueID($request, &$piePayTimes, &$piePayUsersX, &$piePayUsersData)
    {
        $grade = $this->payRepository->getWithGrade($request);

        foreach ($grade as $key => $value) {
            $piePayTimes[] = [
                'name' => $value['item_name'] . ' ' . $value['pay_times_rate'] . '%',
                'value' => $value['pay_times']
            ];

            $piePayUsersX[] = $value['item_name'];
            $piePayUsersData[] = $value['pay_users_rate'];
        }
        $piePayTimes = json_encode($piePayTimes, JSON_UNESCAPED_UNICODE);
        $piePayUsersX = json_encode($piePayUsersX, JSON_UNESCAPED_UNICODE);
        $piePayUsersData = json_encode($piePayUsersData, JSON_UNESCAPED_UNICODE);

        return $grade;
    }

    /**
     * 获取所有    充值type
     */
    public function getPayLogType()
    {
        $type = [];
        $gameID = session('game_id');
        $key = "game_id_{$gameID}_pay_log_type";

        if (Cache::has($key)) {
            $type = Cache::get($key);
        } else {
            $type = $this->payRepository->getWithPayLogType();
            Cache::put($key, $type, 10);
        }
        return $type;
    }
}